/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-01-14
 * Time: 22:26
 */
class ListNode {        // 节点
    public int val; // 值域
    public ListNode next; // 指针域

    public ListNode(int val) {  // 构造方法
        this.val = val;
    }
}
public class MyLinkList {
    public ListNode head;
    //头插法
    public void addFirst(int data) {
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    };
    //尾插法
    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if (this.head == null) {
            head = listNode;
        }else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = listNode;
        }
    };
    //任意位置插入,第一个数据节点为0号下标

    /**
     * 查找index的前一个值的地址并返回
     * @param index
     * @return
     */
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index-1 != 0) {
            cur = cur.next;
        }
        return cur;
    }
    public void addIndex(int index,int data) {
        if (index < 0 || index > size()) {
            System.out.println("位置不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return ;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        cur.next = cur.next.next;
    };
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    };
    //删除第一次出现关键字为key的节点

    /**
     * 找到要删除关键字的前驱
     * @param key
     * @return
     */
    public ListNode findkey(int key) {
        ListNode cur = this.head;
        while (cur.next == null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        System.out.println("没找到");
        return null;
    }
    public void remove(int key) {
        ListNode cur = findIndex(key);
        cur.next = cur.next.next;
    };
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (this.head == null) {
            System.out.println("此链表为空");
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            head = head.next;
        }
    };
    //得到单链表的长度
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    };
    // 打印链表
    public void display() {
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val);
            cur = cur.next;
        }
        System.out.println();
    };
    // 清空链表
    public void clear() {

        while (head != null) {
            ListNode cur = this.head.next;
            head.next = null;
            head = cur;
        }
    };
}
